package com.cxxx.guns.modular.backend.controller;

import com.cxxx.guns.core.base.controller.BaseController;
import com.cxxx.guns.core.support.BeanKit;
import com.cxxx.guns.modular.backend.model.TeachersClass;
import com.cxxx.guns.modular.backend.service.ITeachersClassService;
import com.cxxx.guns.modular.backend.warpper.TeachersWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cxxx.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cxxx.guns.modular.backend.model.Teachers;
import com.cxxx.guns.modular.backend.service.ITeachersService;

import java.util.List;

/**
 * 师资分类控制器
 *
 * @author fengshuonan
 * @Date 2018-09-14 11:10:02
 */
@Controller
@RequestMapping("/teachers")
public class TeachersController extends BaseController {

    private String PREFIX = "/backend/teachers/";

    @Autowired
    private ITeachersService teachersService;

    @Autowired
    private ITeachersClassService iTeachersClassService;

    /**
     * 跳转到师资分类首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "teachers.html";
    }

    /**
     * 跳转到添加师资分类
     */
    @RequestMapping("/teachers_add")
    public String teachersAdd(Model model) {
        List<TeachersClass> list = iTeachersClassService.selectList(null);
        model.addAttribute("list",list);
        return PREFIX + "teachers_add.html";
    }

    /**
     * 跳转到修改师资分类
     */
    @RequestMapping("/teachers_update/{teachersId}")
    public String teachersUpdate(@PathVariable Integer teachersId, Model model) {
        Teachers teachers = teachersService.selectById(teachersId);
        model.addAttribute("item",teachers);
        List<TeachersClass> list = iTeachersClassService.selectList(null);
        model.addAttribute("list",list);
        LogObjectHolder.me().set(teachers);
        return PREFIX + "teachers_edit.html";
    }

    /**
     * 获取师资分类列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Teachers> list = teachersService.selectList(null);
        return  new TeachersWarpper(BeanKit.listToMapList(list)).warp();
    }

    /**
     * 新增师资分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Teachers teachers) {
        teachersService.insert(teachers);
        return SUCCESS_TIP;
    }

    /**
     * 删除师资分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer teachersId) {
        teachersService.deleteById(teachersId);
        return SUCCESS_TIP;
    }

    /**
     * 修改师资分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Teachers teachers) {
        teachersService.updateById(teachers);
        return SUCCESS_TIP;
    }

    /**
     * 师资分类详情
     */
    @RequestMapping(value = "/detail/{teachersId}")
    @ResponseBody
    public Object detail(@PathVariable("teachersId") Integer teachersId) {
        return teachersService.selectById(teachersId);
    }
}
